﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Management;
using System.Text;
using WMI.NET.Abstracts;
using WMI.NET.CIM;

namespace WMI.NET.ComputerSystemHardware.VideoAndMonitor
{
    /// <summary>
    /// [The Win32_DisplayConfiguration WMI class is no longer available for use as of Windows Vista. Instead, use the properties in the Win32_VideoController, Win32_DesktopMonitor, and CIM_VideoControllerResolution classes.]
    /// Windows Server 2003 and Windows XP:  
    /// The Win32_DisplayConfiguration WMI class represents configuration information for the display device on a computer running on a Windows operating system.
    /// Hardware that is not compatible with Windows Display Driver Model (WDDM) returns inaccurate property values for instances of this class.
    /// </summary>
    public class Win32_DisplayConfiguration : CIM_Setting
    {

        private Nullable<UInt32> _BitsPerPel;
        private string _DeviceName;
        private Nullable<UInt32> _DisplayFlags;
        private Nullable<UInt32> _DisplayFrequency;
        private Nullable<UInt32> _DitherType;
        private string _DriverVersion;
        private Nullable<UInt32> _ICMIntent;
        private Nullable<UInt32> _ICMMethod;
        private Nullable<UInt32> _LogPixels;
        private Nullable<UInt32> _PelsHeight;
        private Nullable<UInt32> _PelsWidth;
        private Nullable<UInt32> _SpecificationVersion;

        /// <summary>
        /// Number of bits used to represent the color in this configuration (bits per pixel).
        /// Example: 8
        /// </summary>
        public Nullable<UInt32> BitsPerPel { get { return _BitsPerPel; } }

        /// <summary>
        /// Name of the display device. MaxLen (260) 
        /// </summary>
        public string DeviceName { get { return _DeviceName; } }

        /// <summary>
        /// If TRUE, the display device is color (versus monochrome), and noninterlaced (versus interlaced), by masking its value with the DM_GRAYSCALE and DM_INTERLACED masks respectively.
        /// </summary>
        public Nullable<UInt32> DisplayFlags { get { return _DisplayFlags; } }

        /// <summary>
        /// The vertical refresh rate for the display. The refresh rate for a monitor is the number of times the screen is redrawn per second (frequency). (Hertz)
        /// </summary>
        public Nullable<UInt32> DisplayFrequency { get { return _DisplayFrequency; } }

        /// <summary>
        /// Dither type of the display. This property can assume predefined values of 1 to 5, or driver-defined values from 6 to 256. Line art dithering is a special dithering method that produces well-defined borders between black, white, and gray scalings. It is not suitable for images that include continuous graduations in intensity and hue (such as scanned photographs).
        /// </summary>
        public Nullable<UInt32> DitherType { get { return _DitherType; } } //TODO enum

        /// <summary>
        /// Release version of the display driver.
        /// </summary>
        public string DriverVersion { get { return _DriverVersion; } }

        /// <summary>
        /// Value of one of the three possible color matching methods (intents) that should be used by default. This property is used primarily for non-ICM applications. ICM applications establish intents by using the ICM functions. This property can assume predefined values of 1 to 3, or driver-defined values from 4 to 256.
        /// </summary>
        public Nullable<UInt32> ICMIntent { get { return _ICMIntent; } } //TODO enum

        /// <summary>
        /// How ICM is handled. For a non-ICM application, this property indicates if ICM is enabled or disabled. For ICM applications, the system examines this property to determine which part of the computer system handles ICM support.
        /// </summary>
        public Nullable<UInt32> ICMMethod { get { return _ICMMethod; } } //TODO enum

        /// <summary>
        /// Number of pixels per logical inch. This property is valid only with devices that work with pixels (this excludes devices such as printers).(Pixels)
        /// </summary>
        public Nullable<UInt32> LogPixels { get { return _LogPixels; } }

        /// <summary>
        /// Height of the displayable surface.(Pixels)
        /// </summary>
        public Nullable<UInt32> PelsHeight { get { return _PelsHeight; } }

        /// <summary>
        /// Width of the displayable surface.(Pixels)
        /// </summary>
        public Nullable<UInt32> PelsWidth { get { return _PelsWidth; } }

        /// <summary>
        /// Version number of the initialization data for the Windows display device.
        /// </summary>
        public Nullable<UInt32> SpecificationVersion { get { return _SpecificationVersion; } }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mbo"></param>
        public Win32_DisplayConfiguration(ManagementBaseObject mbo)
            : base(mbo)
        {
            _BitsPerPel = mbo.GetUIntValue("BitsPerPel");
            _DeviceName = mbo.GetStringValue("DeviceName");
            _DisplayFlags = mbo.GetUIntValue("DisplayFlags");
            _DisplayFrequency = mbo.GetUIntValue("DisplayFrequency");
            _DitherType = mbo.GetUIntValue("DitherType");
            _DriverVersion = mbo.GetStringValue("DriverVersion");
            _ICMIntent = mbo.GetUIntValue("ICMIntent");
            _ICMMethod = mbo.GetUIntValue("ICMMethod");
            _LogPixels = mbo.GetUIntValue("LogPixels");
            _PelsHeight = mbo.GetUIntValue("PelsHeight");
            _PelsWidth = mbo.GetUIntValue("PelsWidth");
            _SpecificationVersion = mbo.GetUIntValue("SpecificationVersion");
        }

    }

    /// <summary>
    /// 
    /// </summary>
    public class GetWin32_DisplayConfigurations : Win32Collection<Win32_DisplayConfiguration>
    {

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override ReadOnlyCollection<Win32_DisplayConfiguration> GetCollection()
        {
            List<Win32_DisplayConfiguration> tempCollection = new List<Win32_DisplayConfiguration>();

            foreach (var s in searcher.Get())
            {
                Win32_DisplayConfiguration tempListItem = new Win32_DisplayConfiguration(s);
                tempCollection.Add(tempListItem);
            }

            return tempCollection.AsReadOnly();
        }

        /// <summary>
        /// 
        /// </summary>
        public GetWin32_DisplayConfigurations() : base("SELECT * FROM Win32_DisplayConfiguration") { }

    }

}
